package com.xunshangwang.pinhaohuo.tools;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;

import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.entity.Goods;
import com.xunshangwang.pinhaohuo.entity.Pin;

import java.text.DecimalFormat;

/**
 * Created by xzp on 16/1/15.
 */
public class CommonTool {
    public static final String SHARE_GROUP_URL="http://d123.xunshangwang.cn/html/ppp.html?group_order_id=";
    public static final int dpToPx(Context context,int dp){
        return (int) (context.getResources().getDisplayMetrics().density*dp+0.5);
    }
    public static final SpannableStringBuilder getPriceFormatString(Context context,Goods goods,int groupBuy){
        if(goods==null)
            return null;
        SpannableStringBuilder builder=new SpannableStringBuilder();
        DecimalFormat format=new DecimalFormat("0.##");
        SpannableString priceString;
        if(groupBuy==0){
            builder.append("单独购买价: ");
            priceString=SpannableString.valueOf(format.format(goods.getAlone_price()));
        }
        else {
            SpannableString spannableString=SpannableString.valueOf(goods.getGroup_number()+"人拼团价: ");
            spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)),0,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.append(spannableString);
            priceString=SpannableString.valueOf(format.format(goods.getGroup_price()));
        }
        SpannableString unitString=SpannableString.valueOf("￥");
        unitString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append(unitString);
        priceString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)), 0, priceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        priceString.setSpan(new AbsoluteSizeSpan(18, true), 0, priceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append(priceString);
        builder.append(" ");
        SpannableString marketPriceString=SpannableString.valueOf("￥"+format.format(goods.getMarket_price()));
        marketPriceString.setSpan(new StrikethroughSpan(), 0, marketPriceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        marketPriceString.setSpan(new AbsoluteSizeSpan(12, true), 0, marketPriceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.append(marketPriceString);
        return builder;
    }
//    public static final SpannableStringBuilder getPriceFormatString(Context context,Pin pin){
//        SpannableStringBuilder builder=new SpannableStringBuilder();
//        DecimalFormat format=new DecimalFormat("0.##");
//
//        SpannableString spannableString=SpannableString.valueOf(pin.getGroup_number()+"人拼团价: ");
//        spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)),0,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.append(spannableString);
//        SpannableString priceString=SpannableString.valueOf(format.format(pin.getGroup_price()));
//
//        SpannableString unitString=SpannableString.valueOf("￥");
//        unitString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.append(unitString);
//        priceString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.red)), 0, priceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        priceString.setSpan(new AbsoluteSizeSpan(18, true), 0, priceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.append(priceString);
//        builder.append(" ");
//        SpannableString marketPriceString=SpannableString.valueOf("￥"+format.format(pin.getMarket_price()));
//        marketPriceString.setSpan(new StrikethroughSpan(), 0, marketPriceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        marketPriceString.setSpan(new AbsoluteSizeSpan(12, true), 0, marketPriceString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        builder.append(marketPriceString);
//        return builder;
//    }
}
